<?php

/**
 * 
 *
 * @version 1.107
 * @package entity
 */
class UsuArioModel extends Db2PhpEntityBase implements Db2PhpEntityModificationTracking {
	private static $CLASS_NAME='UsuArioModel';
	const SQL_IDENTIFIER_QUOTE='`';
	const SQL_TABLE_NAME='usuario';
	const SQL_INSERT='INSERT INTO `usuario` (`id`,`Username`,`Nombres`,`Apellidos`,`Telefono`,`Email`,`Password`,`Saldo`,`Eliminado`,`Activo`,`Idaprobador`) VALUES (?,?,?,?,?,?,?,?,?,?,?)';
	const SQL_INSERT_AUTOINCREMENT='INSERT INTO `usuario` (`Username`,`Nombres`,`Apellidos`,`Telefono`,`Email`,`Password`,`Saldo`,`Eliminado`,`Activo`,`Idaprobador`) VALUES (?,?,?,?,?,?,?,?,?,?)';
	const SQL_UPDATE='UPDATE `usuario` SET `id`=?,`Username`=?,`Nombres`=?,`Apellidos`=?,`Telefono`=?,`Email`=?,`Password`=?,`Saldo`=?,`Eliminado`=?,`Activo`=?,`Idaprobador`=? WHERE `id`=?';
	const SQL_SELECT_PK='SELECT * FROM `usuario` WHERE `id`=?';
	const SQL_DELETE_PK='DELETE FROM `usuario` WHERE `id`=?';
	const FIELD_ID=-1913139237;
	const FIELD_USERNAME=307142582;
	const FIELD_NOMBRES=-906873110;
	const FIELD_APELLIDOS=1613980789;
	const FIELD_TELEFONO=-787643948;
	const FIELD_EMAIL=-147873604;
	const FIELD_PASSWORD=1789841787;
	const FIELD_SALDO=-135291383;
	const FIELD_ELIMINADO=-421124772;
	const FIELD_ACTIVO=-412299792;
	const FIELD_IDAPROBADOR=-359457823;
	private static $PRIMARY_KEYS=array(self::FIELD_ID);
	private static $AUTOINCREMENT_FIELDS=array(self::FIELD_ID);
	private static $FIELD_NAMES=array(
		self::FIELD_ID=>'id',
		self::FIELD_USERNAME=>'Username',
		self::FIELD_NOMBRES=>'Nombres',
		self::FIELD_APELLIDOS=>'Apellidos',
		self::FIELD_TELEFONO=>'Telefono',
		self::FIELD_EMAIL=>'Email',
		self::FIELD_PASSWORD=>'Password',
		self::FIELD_SALDO=>'Saldo',
		self::FIELD_ELIMINADO=>'Eliminado',
		self::FIELD_ACTIVO=>'Activo',
		self::FIELD_IDAPROBADOR=>'Idaprobador');
	private static $PROPERTY_NAMES=array(
		self::FIELD_ID=>'id',
		self::FIELD_USERNAME=>'userName',
		self::FIELD_NOMBRES=>'nOmbRes',
		self::FIELD_APELLIDOS=>'apeLLidos',
		self::FIELD_TELEFONO=>'telEfOno',
		self::FIELD_EMAIL=>'email',
		self::FIELD_PASSWORD=>'password',
		self::FIELD_SALDO=>'sAldo',
		self::FIELD_ELIMINADO=>'eliMinaDo',
		self::FIELD_ACTIVO=>'actIvO',
		self::FIELD_IDAPROBADOR=>'idaProbAdoR');
	private static $PROPERTY_TYPES=array(
		self::FIELD_ID=>Db2PhpEntity::PHP_TYPE_INT,
		self::FIELD_USERNAME=>Db2PhpEntity::PHP_TYPE_STRING,
		self::FIELD_NOMBRES=>Db2PhpEntity::PHP_TYPE_STRING,
		self::FIELD_APELLIDOS=>Db2PhpEntity::PHP_TYPE_STRING,
		self::FIELD_TELEFONO=>Db2PhpEntity::PHP_TYPE_STRING,
		self::FIELD_EMAIL=>Db2PhpEntity::PHP_TYPE_STRING,
		self::FIELD_PASSWORD=>Db2PhpEntity::PHP_TYPE_STRING,
		self::FIELD_SALDO=>Db2PhpEntity::PHP_TYPE_FLOAT,
		self::FIELD_ELIMINADO=>Db2PhpEntity::PHP_TYPE_BOOL,
		self::FIELD_ACTIVO=>Db2PhpEntity::PHP_TYPE_BOOL,
		self::FIELD_IDAPROBADOR=>Db2PhpEntity::PHP_TYPE_INT);
	private static $FIELD_TYPES=array(
		self::FIELD_ID=>array(Db2PhpEntity::JDBC_TYPE_INTEGER,10,0,false),
		self::FIELD_USERNAME=>array(Db2PhpEntity::JDBC_TYPE_VARCHAR,255,0,false),
		self::FIELD_NOMBRES=>array(Db2PhpEntity::JDBC_TYPE_VARCHAR,45,0,true),
		self::FIELD_APELLIDOS=>array(Db2PhpEntity::JDBC_TYPE_VARCHAR,45,0,true),
		self::FIELD_TELEFONO=>array(Db2PhpEntity::JDBC_TYPE_VARCHAR,45,0,true),
		self::FIELD_EMAIL=>array(Db2PhpEntity::JDBC_TYPE_VARCHAR,45,0,true),
		self::FIELD_PASSWORD=>array(Db2PhpEntity::JDBC_TYPE_VARCHAR,255,0,false),
		self::FIELD_SALDO=>array(Db2PhpEntity::JDBC_TYPE_BIGINT,19,0,false),
		self::FIELD_ELIMINADO=>array(Db2PhpEntity::JDBC_TYPE_BIT,0,0,false),
		self::FIELD_ACTIVO=>array(Db2PhpEntity::JDBC_TYPE_BIT,0,0,false),
		self::FIELD_IDAPROBADOR=>array(Db2PhpEntity::JDBC_TYPE_INTEGER,10,0,false));
	private static $DEFAULT_VALUES=array(
		self::FIELD_ID=>null,
		self::FIELD_USERNAME=>'',
		self::FIELD_NOMBRES=>null,
		self::FIELD_APELLIDOS=>null,
		self::FIELD_TELEFONO=>null,
		self::FIELD_EMAIL=>null,
		self::FIELD_PASSWORD=>'',
		self::FIELD_SALDO=>0,
		self::FIELD_ELIMINADO=>'0',
		self::FIELD_ACTIVO=>'0',
		self::FIELD_IDAPROBADOR=>0);
	private $id;
	private $userName;
	private $nOmbRes;
	private $apeLLidos;
	private $telEfOno;
	private $email;
	private $password;
	private $sAldo;
	private $eliMinaDo;
	private $actIvO;
	private $idaProbAdoR;

	/**
	 * set value for id 
	 *
	 * type:INT,size:10,default:null,primary,unique,autoincrement
	 *
	 * @param mixed $id
	 * @return UsuArioModel
	 */
	public function &setId($id) {
		$this->notifyChanged(self::FIELD_ID,$this->id,$id);
		$this->id=$id;
		return $this;
	}

	/**
	 * get value for id 
	 *
	 * type:INT,size:10,default:null,primary,unique,autoincrement
	 *
	 * @return mixed
	 */
	public function getId() {
		return $this->id;
	}

	/**
	 * set value for Username 
	 *
	 * type:VARCHAR,size:255,default:null
	 *
	 * @param mixed $userName
	 * @return UsuArioModel
	 */
	public function &setUserName($userName) {
		$this->notifyChanged(self::FIELD_USERNAME,$this->userName,$userName);
		$this->userName=$userName;
		return $this;
	}

	/**
	 * get value for Username 
	 *
	 * type:VARCHAR,size:255,default:null
	 *
	 * @return mixed
	 */
	public function getUserName() {
		return $this->userName;
	}

	/**
	 * set value for Nombres 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @param mixed $nOmbRes
	 * @return UsuArioModel
	 */
	public function &setNOmbRes($nOmbRes) {
		$this->notifyChanged(self::FIELD_NOMBRES,$this->nOmbRes,$nOmbRes);
		$this->nOmbRes=$nOmbRes;
		return $this;
	}

	/**
	 * get value for Nombres 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @return mixed
	 */
	public function getNOmbRes() {
		return $this->nOmbRes;
	}

	/**
	 * set value for Apellidos 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @param mixed $apeLLidos
	 * @return UsuArioModel
	 */
	public function &setApeLLidos($apeLLidos) {
		$this->notifyChanged(self::FIELD_APELLIDOS,$this->apeLLidos,$apeLLidos);
		$this->apeLLidos=$apeLLidos;
		return $this;
	}

	/**
	 * get value for Apellidos 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @return mixed
	 */
	public function getApeLLidos() {
		return $this->apeLLidos;
	}

	/**
	 * set value for Telefono 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @param mixed $telEfOno
	 * @return UsuArioModel
	 */
	public function &setTelEfOno($telEfOno) {
		$this->notifyChanged(self::FIELD_TELEFONO,$this->telEfOno,$telEfOno);
		$this->telEfOno=$telEfOno;
		return $this;
	}

	/**
	 * get value for Telefono 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @return mixed
	 */
	public function getTelEfOno() {
		return $this->telEfOno;
	}

	/**
	 * set value for Email 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @param mixed $email
	 * @return UsuArioModel
	 */
	public function &setEmail($email) {
		$this->notifyChanged(self::FIELD_EMAIL,$this->email,$email);
		$this->email=$email;
		return $this;
	}

	/**
	 * get value for Email 
	 *
	 * type:VARCHAR,size:45,default:null,nullable
	 *
	 * @return mixed
	 */
	public function getEmail() {
		return $this->email;
	}

	/**
	 * set value for Password 
	 *
	 * type:VARCHAR,size:255,default:null
	 *
	 * @param mixed $password
	 * @return UsuArioModel
	 */
	public function &setPassword($password) {
		$this->notifyChanged(self::FIELD_PASSWORD,$this->password,$password);
		$this->password=$password;
		return $this;
	}

	/**
	 * get value for Password 
	 *
	 * type:VARCHAR,size:255,default:null
	 *
	 * @return mixed
	 */
	public function getPassword() {
		return $this->password;
	}

	/**
	 * set value for Saldo 
	 *
	 * type:BIGINT,size:19,default:null
	 *
	 * @param mixed $sAldo
	 * @return UsuArioModel
	 */
	public function &setSAldo($sAldo) {
		$this->notifyChanged(self::FIELD_SALDO,$this->sAldo,$sAldo);
		$this->sAldo=$sAldo;
		return $this;
	}

	/**
	 * get value for Saldo 
	 *
	 * type:BIGINT,size:19,default:null
	 *
	 * @return mixed
	 */
	public function getSAldo() {
		return $this->sAldo;
	}

	/**
	 * set value for Eliminado 
	 *
	 * type:BIT,size:0,default:0
	 *
	 * @param mixed $eliMinaDo
	 * @return UsuArioModel
	 */
	public function &setEliMinaDo($eliMinaDo) {
		$this->notifyChanged(self::FIELD_ELIMINADO,$this->eliMinaDo,$eliMinaDo);
		$this->eliMinaDo=$eliMinaDo;
		return $this;
	}

	/**
	 * get value for Eliminado 
	 *
	 * type:BIT,size:0,default:0
	 *
	 * @return mixed
	 */
	public function getEliMinaDo() {
		return $this->eliMinaDo;
	}

	/**
	 * set value for Activo 
	 *
	 * type:BIT,size:0,default:0
	 *
	 * @param mixed $actIvO
	 * @return UsuArioModel
	 */
	public function &setActIvO($actIvO) {
		$this->notifyChanged(self::FIELD_ACTIVO,$this->actIvO,$actIvO);
		$this->actIvO=$actIvO;
		return $this;
	}

	/**
	 * get value for Activo 
	 *
	 * type:BIT,size:0,default:0
	 *
	 * @return mixed
	 */
	public function getActIvO() {
		return $this->actIvO;
	}

	/**
	 * set value for Idaprobador 
	 *
	 * type:INT,size:10,default:null,index
	 *
	 * @param mixed $idaProbAdoR
	 * @return UsuArioModel
	 */
	public function &setIdaProbAdoR($idaProbAdoR) {
		$this->notifyChanged(self::FIELD_IDAPROBADOR,$this->idaProbAdoR,$idaProbAdoR);
		$this->idaProbAdoR=$idaProbAdoR;
		return $this;
	}

	/**
	 * get value for Idaprobador 
	 *
	 * type:INT,size:10,default:null,index
	 *
	 * @return mixed
	 */
	public function getIdaProbAdoR() {
		return $this->idaProbAdoR;
	}

	/**
	 * Get table name
	 *
	 * @return string
	 */
	public static function getTableName() {
		return self::SQL_TABLE_NAME;
	}

	/**
	 * Get array with field id as index and field name as value
	 *
	 * @return array
	 */
	public static function getFieldNames() {
		return self::$FIELD_NAMES;
	}

	/**
	 * Get array with field id as index and property name as value
	 *
	 * @return array
	 */
	public static function getPropertyNames() {
		return self::$PROPERTY_NAMES;
	}

	/**
	 * get the field name for the passed field id.
	 *
	 * @param int $fieldId
	 * @param bool $fullyQualifiedName true if field name should be qualified by table name
	 * @return string field name for the passed field id, null if the field doesn't exist
	 */
	public static function getFieldNameByFieldId($fieldId, $fullyQualifiedName=true) {
		if (!array_key_exists($fieldId, self::$FIELD_NAMES)) {
			return null;
		}
		$fieldName=self::SQL_IDENTIFIER_QUOTE . self::$FIELD_NAMES[$fieldId] . self::SQL_IDENTIFIER_QUOTE;
		if ($fullyQualifiedName) {
			return self::SQL_IDENTIFIER_QUOTE . self::SQL_TABLE_NAME . self::SQL_IDENTIFIER_QUOTE . '.' . $fieldName;
		}
		return $fieldName;
	}

	/**
	 * Get array with field ids of identifiers
	 *
	 * @return array
	 */
	public static function getIdentifierFields() {
		return self::$PRIMARY_KEYS;
	}

	/**
	 * Get array with field ids of autoincrement fields
	 *
	 * @return array
	 */
	public static function getAutoincrementFields() {
		return self::$AUTOINCREMENT_FIELDS;
	}

	/**
	 * Get array with field id as index and property type as value
	 *
	 * @return array
	 */
	public static function getPropertyTypes() {
		return self::$PROPERTY_TYPES;
	}

	/**
	 * Get array with field id as index and field type as value
	 *
	 * @return array
	 */
	public static function getFieldTypes() {
		return self::$FIELD_TYPES;
	}

	/**
	 * Assign default values according to table
	 * 
	 */
	public function assignDefaultValues() {
		$this->assignByArray(self::$DEFAULT_VALUES);
	}


	/**
	 * return hash with the field name as index and the field value as value.
	 *
	 * @return array
	 */
	public function toHash() {
		$array=$this->toArray();
		$hash=array();
		foreach ($array as $fieldId=>$value) {
			$hash[self::$FIELD_NAMES[$fieldId]]=$value;
		}
		return $hash;
	}

	/**
	 * return array with the field id as index and the field value as value.
	 *
	 * @return array
	 */
	public function toArray() {
		return array(
			self::FIELD_ID=>$this->getId(),
			self::FIELD_USERNAME=>$this->getUserName(),
			self::FIELD_NOMBRES=>$this->getNOmbRes(),
			self::FIELD_APELLIDOS=>$this->getApeLLidos(),
			self::FIELD_TELEFONO=>$this->getTelEfOno(),
			self::FIELD_EMAIL=>$this->getEmail(),
			self::FIELD_PASSWORD=>$this->getPassword(),
			self::FIELD_SALDO=>$this->getSAldo(),
			self::FIELD_ELIMINADO=>$this->getEliMinaDo(),
			self::FIELD_ACTIVO=>$this->getActIvO(),
			self::FIELD_IDAPROBADOR=>$this->getIdaProbAdoR());
	}


	/**
	 * return array with the field id as index and the field value as value for the identifier fields.
	 *
	 * @return array
	 */
	public function getPrimaryKeyValues() {
		return array(
			self::FIELD_ID=>$this->getId());
	}

	/**
	 * cached statements
	 *
	 * @var array<string,array<string,PDOStatement>>
	 */
	private static $stmts=array();
	private static $cacheStatements=true;
	
	/**
	 * prepare passed string as statement or return cached if enabled and available
	 *
	 * @param PDO $db
	 * @param string $statement
	 * @return PDOStatement
	 */
	protected static function prepareStatement(PDO $db, $statement) {
		if(self::isCacheStatements()) {
			if (in_array($statement, array(self::SQL_INSERT, self::SQL_INSERT_AUTOINCREMENT, self::SQL_UPDATE, self::SQL_SELECT_PK, self::SQL_DELETE_PK))) {
				$dbInstanceId=spl_object_hash($db);
				if (empty(self::$stmts[$statement][$dbInstanceId])) {
					self::$stmts[$statement][$dbInstanceId]=$db->prepare($statement);
				}
				return self::$stmts[$statement][$dbInstanceId];
			}
		}
		return $db->prepare($statement);
	}

	/**
	 * Enable statement cache
	 *
	 * @param bool $cache
	 */
	public static function setCacheStatements($cache) {
		self::$cacheStatements=true==$cache;
	}

	/**
	 * Check if statement cache is enabled
	 *
	 * @return bool
	 */
	public static function isCacheStatements() {
		return self::$cacheStatements;
	}
	
	/**
	 * check if this instance exists in the database
	 *
	 * @param PDO $db
	 * @return bool
	 */
	public function existsInDatabase(PDO $db) {
		$filter=array();
		foreach ($this->getPrimaryKeyValues() as $fieldId=>$value) {
			$filter[]=new DFC($fieldId, $value, DFC::EXACT_NULLSAFE);
		}
		return 0!=count(self::findByFilter($db, $filter, true));
	}
	
	/**
	 * Update to database if exists, otherwise insert
	 *
	 * @param PDO $db
	 * @return mixed
	 */
	public function updateInsertToDatabase(PDO $db) {
		if ($this->existsInDatabase($db)) {
			return $this->updateToDatabase($db);
		} else {
			return $this->insertIntoDatabase($db);
		}
	}

	/**
	 * Query by Example.
	 *
	 * Match by attributes of passed example instance and return matched rows as an array of UsuArioModel instances
	 *
	 * @param PDO $db a PDO Database instance
	 * @param UsuArioModel $example an example instance defining the conditions. All non-null properties will be considered a constraint, null values will be ignored.
	 * @param boolean $and true if conditions should be and'ed, false if they should be or'ed
	 * @param array $sort array of DSC instances
	 * @return UsuArioModel[]
	 */
	public static function findByExample(PDO $db,UsuArioModel $example, $and=true, $sort=null) {
		$exampleValues=$example->toArray();
		$filter=array();
		foreach ($exampleValues as $fieldId=>$value) {
			if (null!==$value) {
				$filter[$fieldId]=$value;
			}
		}
		return self::findByFilter($db, $filter, $and, $sort);
	}

	/**
	 * Query by filter.
	 *
	 * The filter can be either an hash with the field id as index and the value as filter value,
	 * or a array of DFC instances.
	 *
	 * Will return matched rows as an array of UsuArioModel instances.
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $filter array of DFC instances defining the conditions
	 * @param boolean $and true if conditions should be and'ed, false if they should be or'ed
	 * @param array $sort array of DSC instances
	 * @return UsuArioModel[]
	 */
	public static function findByFilter(PDO $db, $filter, $and=true, $sort=null) {
		if (!($filter instanceof DFCInterface)) {
			$filter=new DFCAggregate($filter, $and);
		}
		$sql='SELECT * FROM `usuario`'
		. self::buildSqlWhere($filter, $and, false, true)
		. self::buildSqlOrderBy($sort);

		$stmt=self::prepareStatement($db, $sql);
		self::bindValuesForFilter($stmt, $filter);
		return self::fromStatement($stmt);
	}

	/**
	 * Will execute the passed statement and return the result as an array of UsuArioModel instances
	 *
	 * @param PDOStatement $stmt
	 * @return UsuArioModel[]
	 */
	public static function fromStatement(PDOStatement $stmt) {
		$affected=$stmt->execute();
		if (false===$affected) {
			$stmt->closeCursor();
			throw new Exception($stmt->errorCode() . ':' . var_export($stmt->errorInfo(), true), 0);
		}
		return self::fromExecutedStatement($stmt);
	}

	/**
	 * returns the result as an array of UsuArioModel instances without executing the passed statement
	 *
	 * @param PDOStatement $stmt
	 * @return UsuArioModel[]
	 */
	public static function fromExecutedStatement(PDOStatement $stmt) {
		$resultInstances=array();
		while($result=$stmt->fetch(PDO::FETCH_ASSOC)) {
			$o=new UsuArioModel();
			$o->assignByHash($result);
			$o->notifyPristine();
			$resultInstances[]=$o;
		}
		$stmt->closeCursor();
		return $resultInstances;
	}

	/**
	 * Get sql WHERE part from filter.
	 *
	 * @param array $filter
	 * @param bool $and
	 * @param bool $fullyQualifiedNames true if field names should be qualified by table name
	 * @param bool $prependWhere true if WHERE should be prepended to conditions
	 * @return string
	 */
	public static function buildSqlWhere($filter, $and, $fullyQualifiedNames=true, $prependWhere=false) {
		if (!($filter instanceof DFCInterface)) {
			$filter=new DFCAggregate($filter, $and);
		}
		return $filter->buildSqlWhere(new self::$CLASS_NAME, $fullyQualifiedNames, $prependWhere);
	}

	/**
	 * get sql ORDER BY part from DSCs
	 *
	 * @param array $sort array of DSC instances
	 * @return string
	 */
	protected static function buildSqlOrderBy($sort) {
		return DSC::buildSqlOrderBy(new self::$CLASS_NAME, $sort);
	}

	/**
	 * bind values from filter to statement
	 *
	 * @param PDOStatement $stmt
	 * @param DFCInterface $filter
	 */
	public static function bindValuesForFilter(PDOStatement &$stmt, DFCInterface $filter) {
		$filter->bindValuesForFilter(new self::$CLASS_NAME, $stmt);
	}

	/**
	 * Execute select query and return matched rows as an array of UsuArioModel instances.
	 *
	 * The query should of course be on the table for this entity class and return all fields.
	 *
	 * @param PDO $db a PDO Database instance
	 * @param string $sql
	 * @return UsuArioModel[]
	 */
	public static function findBySql(PDO $db, $sql) {
		$stmt=$db->query($sql);
		return self::fromExecutedStatement($stmt);
	}

	/**
	 * Delete rows matching the filter
	 *
	 * The filter can be either an hash with the field id as index and the value as filter value,
	 * or a array of DFC instances.
	 *
	 * @param PDO $db
	 * @param array $filter
	 * @param bool $and
	 * @return mixed
	 */
	public static function deleteByFilter(PDO $db, $filter, $and=true) {
		if (!($filter instanceof DFCInterface)) {
			$filter=new DFCAggregate($filter, $and);
		}
		if (0==count($filter)) {
			throw new InvalidArgumentException('refusing to delete without filter'); // just comment out this line if you are brave
		}
		$sql='DELETE FROM `usuario`'
		. self::buildSqlWhere($filter, $and, false, true);
		$stmt=self::prepareStatement($db, $sql);
		self::bindValuesForFilter($stmt, $filter);
		$affected=$stmt->execute();
		if (false===$affected) {
			$stmt->closeCursor();
			throw new Exception($stmt->errorCode() . ':' . var_export($stmt->errorInfo(), true), 0);
		}
		$stmt->closeCursor();
		return $affected;
	}

	/**
	 * Assign values from array with the field id as index and the value as value
	 *
	 * @param array $array
	 */
	public function assignByArray($array) {
		$result=array();
		foreach ($array as $fieldId=>$value) {
			$result[self::$FIELD_NAMES[$fieldId]]=$value;
		}
		$this->assignByHash($result);
	}

	/**
	 * Assign values from hash where the indexes match the tables field names
	 *
	 * @param array $result
	 */
	public function assignByHash($result) {
		$this->setId($result['id']);
		$this->setUserName($result['Username']);
		$this->setNOmbRes($result['Nombres']);
		$this->setApeLLidos($result['Apellidos']);
		$this->setTelEfOno($result['Telefono']);
		$this->setEmail($result['Email']);
		$this->setPassword($result['Password']);
		$this->setSAldo($result['Saldo']);
		$this->setEliMinaDo($result['Eliminado']);
		$this->setActIvO($result['Activo']);
		$this->setIdaProbAdoR($result['Idaprobador']);
	}

	/**
	 * Get element instance by it's primary key(s).
	 * Will return null if no row was matched.
	 *
	 * @param PDO $db
	 * @return UsuArioModel
	 */
	public static function findById(PDO $db,$id) {
		$stmt=self::prepareStatement($db,self::SQL_SELECT_PK);
		$stmt->bindValue(1,$id);
		$affected=$stmt->execute();
		if (false===$affected) {
			$stmt->closeCursor();
			throw new Exception($stmt->errorCode() . ':' . var_export($stmt->errorInfo(), true), 0);
		}
		$result=$stmt->fetch(PDO::FETCH_ASSOC);
		$stmt->closeCursor();
		if(!$result) {
			return null;
		}
		$o=new UsuArioModel();
		$o->assignByHash($result);
		$o->notifyPristine();
		return $o;
	}

	/**
	 * Bind all values to statement
	 *
	 * @param PDOStatement $stmt
	 */
	protected function bindValues(PDOStatement &$stmt) {
		$stmt->bindValue(1,$this->getId());
		$stmt->bindValue(2,$this->getUserName());
		$stmt->bindValue(3,$this->getNOmbRes());
		$stmt->bindValue(4,$this->getApeLLidos());
		$stmt->bindValue(5,$this->getTelEfOno());
		$stmt->bindValue(6,$this->getEmail());
		$stmt->bindValue(7,$this->getPassword());
		$stmt->bindValue(8,$this->getSAldo());
		$stmt->bindValue(9,$this->getEliMinaDo());
		$stmt->bindValue(10,$this->getActIvO());
		$stmt->bindValue(11,$this->getIdaProbAdoR());
	}


	/**
	 * Insert this instance into the database
	 *
	 * @param PDO $db
	 * @return mixed
	 */
	public function insertIntoDatabase(PDO $db) {
		if (null===$this->getId()) {
			$stmt=self::prepareStatement($db,self::SQL_INSERT_AUTOINCREMENT);
			$stmt->bindValue(1,$this->getUserName());
			$stmt->bindValue(2,$this->getNOmbRes());
			$stmt->bindValue(3,$this->getApeLLidos());
			$stmt->bindValue(4,$this->getTelEfOno());
			$stmt->bindValue(5,$this->getEmail());
			$stmt->bindValue(6,$this->getPassword());
			$stmt->bindValue(7,$this->getSAldo());
			$stmt->bindValue(8,$this->getEliMinaDo());
			$stmt->bindValue(9,$this->getActIvO());
			$stmt->bindValue(10,$this->getIdaProbAdoR());
		} else {
			$stmt=self::prepareStatement($db,self::SQL_INSERT);
			$this->bindValues($stmt);
		}
		$affected=$stmt->execute();
		if (false===$affected) {
			$stmt->closeCursor();
			throw new Exception($stmt->errorCode() . ':' . var_export($stmt->errorInfo(), true), 0);
		}
		$lastInsertId=$db->lastInsertId();
		if (false!==$lastInsertId) {
			$this->setId($lastInsertId);
		}
		$stmt->closeCursor();
		$this->notifyPristine();
		return $affected;
	}


	/**
	 * Update this instance into the database
	 *
	 * @param PDO $db
	 * @return mixed
	 */
	public function updateToDatabase(PDO $db) {
		$stmt=self::prepareStatement($db,self::SQL_UPDATE);
		$this->bindValues($stmt);
		$stmt->bindValue(12,$this->getId());
		$affected=$stmt->execute();
		if (false===$affected) {
			$stmt->closeCursor();
			throw new Exception($stmt->errorCode() . ':' . var_export($stmt->errorInfo(), true), 0);
		}
		$stmt->closeCursor();
		$this->notifyPristine();
		return $affected;
	}


	/**
	 * Delete this instance from the database
	 *
	 * @param PDO $db
	 * @return mixed
	 */
	public function deleteFromDatabase(PDO $db) {
		$stmt=self::prepareStatement($db,self::SQL_DELETE_PK);
		$stmt->bindValue(1,$this->getId());
		$affected=$stmt->execute();
		if (false===$affected) {
			$stmt->closeCursor();
			throw new Exception($stmt->errorCode() . ':' . var_export($stmt->errorInfo(), true), 0);
		}
		$stmt->closeCursor();
		return $affected;
	}

	/**
	 * Fetch CLaveModel's which this UsuArioModel references.
	 * `usuario`.`id` -> `clave`.`Idusuario`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return CLaveModel[]
	 */
	public function fetchCLaveModelCollection(PDO $db, $sort=null) {
		$filter=array(CLaveModel::FIELD_IDUSUARIO=>$this->getId());
		return CLaveModel::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch CueNTaModel's which this UsuArioModel references.
	 * `usuario`.`id` -> `cuenta`.`idpropietario`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return CueNTaModel[]
	 */
	public function fetchCueNTaModelCollection(PDO $db, $sort=null) {
		$filter=array(CueNTaModel::FIELD_IDPROPIETARIO=>$this->getId());
		return CueNTaModel::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch CueNTaModel1's which this UsuArioModel references.
	 * `usuario`.`id` -> `cuenta`.`idcreador`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return CueNTaModel1[]
	 */
	public function fetchCueNTaModel1Collection(PDO $db, $sort=null) {
		$filter=array(CueNTaModel1::FIELD_IDCREADOR=>$this->getId());
		return CueNTaModel1::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch LogModel's which this UsuArioModel references.
	 * `usuario`.`id` -> `log`.`idusuario`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return LogModel[]
	 */
	public function fetchLogModelCollection(PDO $db, $sort=null) {
		$filter=array(LogModel::FIELD_IDUSUARIO=>$this->getId());
		return LogModel::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch TransACcIonModel's which this UsuArioModel references.
	 * `usuario`.`id` -> `transaccion`.`idusuario_paga`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return TransACcIonModel[]
	 */
	public function fetchTransACcIonModelCollection(PDO $db, $sort=null) {
		$filter=array(TransACcIonModel::FIELD_IDUSUARIO_PAGA=>$this->getId());
		return TransACcIonModel::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch TransACcIonModel1's which this UsuArioModel references.
	 * `usuario`.`id` -> `transaccion`.`idusuario_aprueba`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return TransACcIonModel1[]
	 */
	public function fetchTransACcIonModel1Collection(PDO $db, $sort=null) {
		$filter=array(TransACcIonModel1::FIELD_IDUSUARIO_APRUEBA=>$this->getId());
		return TransACcIonModel1::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch UsuArioModel's which this UsuArioModel references.
	 * `usuario`.`id` -> `usuario`.`Idaprobador`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return UsuArioModel[]
	 */
	public function fetchUsuArioModelCollection(PDO $db, $sort=null) {
		$filter=array(UsuArioModel::FIELD_IDAPROBADOR=>$this->getId());
		return UsuArioModel::findByFilter($db, $filter, true, $sort);
	}

	/**
	 * Fetch UsuArioModel which references this UsuArioModel. Will return null in case reference is invalid.
	 * `usuario`.`Idaprobador` -> `usuario`.`id`
	 *
	 * @param PDO $db a PDO Database instance
	 * @param array $sort array of DSC instances
	 * @return UsuArioModel
	 */
	public function fetchUsuArioModel(PDO $db, $sort=null) {
		$filter=array(UsuArioModel::FIELD_ID=>$this->getIdaProbAdoR());
		$result=UsuArioModel::findByFilter($db, $filter, true, $sort);
		return empty($result) ? null : $result[0];
	}


	/**
	 * get element as DOM Document
	 *
	 * @return DOMDocument
	 */
	public function toDOM() {
		return self::hashToDomDocument($this->toHash(), 'UsuArioModel');
	}

	/**
	 * get single UsuArioModel instance from a DOMElement
	 *
	 * @param DOMElement $node
	 * @return UsuArioModel
	 */
	public static function fromDOMElement(DOMElement $node) {
		$o=new UsuArioModel();
		$o->assignByHash(self::domNodeToHash($node, self::$FIELD_NAMES, self::$DEFAULT_VALUES, self::$FIELD_TYPES));
			$o->notifyPristine();
		return $o;
	}

	/**
	 * get all instances of UsuArioModel from the passed DOMDocument
	 *
	 * @param DOMDocument $doc
	 * @return UsuArioModel[]
	 */
	public static function fromDOMDocument(DOMDocument $doc) {
		$instances=array();
		foreach ($doc->getElementsByTagName('UsuArioModel') as $node) {
			$instances[]=self::fromDOMElement($node);
		}
		return $instances;
	}

}
?>